package cn.kgm.makeGodV2.relationship;

/**
 * @Version 1.8
 * @Author: Kappi
 * @Date: 2025-10-13 14:09
 * @Description: cn.kgm.makeGodV2.relationship.EmotionManager
 */

import cn.kgm.makeGodV2.emotion.PersonalityTrait;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 角色情感管理器
 */
public class EmotionManager {
    private final String characterId;
    private final PersonalityTrait personality;
    private final Map<String, UnidirectionalRelationship> relationships; // targetId -> relationship

    public EmotionManager(String characterId, PersonalityTrait personality) {
        this.characterId = characterId;
        this.personality = personality;
        this.relationships = new HashMap<>();
    }

    /**
     * 获取或创建关系
     */
    public UnidirectionalRelationship getOrCreateRelationship(String targetId) {
        return relationships.computeIfAbsent(targetId,
                id -> new UnidirectionalRelationship(characterId, id, RelationshipType.STRANGER, 0));
    }

    /**
     * 获取关系
     */
    public UnidirectionalRelationship getRelationship(String targetId) {
        return relationships.get(targetId);
    }

    /**
     * 改变对目标的情感强度
     */
    public void changeRelationship(String targetId, int delta, String reason) {
        UnidirectionalRelationship relationship = getOrCreateRelationship(targetId);

        System.out.printf("%s 对 %s 的情感变化: %s (%+d)%n",
                characterId, targetId, reason, delta);

        relationship.changeIntensity(delta, personality);

        // 应用特殊性格效果
        applySpecialPersonalityEffects(targetId, delta, relationship);
    }

    /**
     * 应用特殊性格效果
     */
    private void applySpecialPersonalityEffects(String targetId, int delta,
                                                UnidirectionalRelationship relationship) {
        // 睚眦必报：受到负面对待时，仇恨加倍
        if (isVindictive() && delta < -5) {
            System.out.printf("  [睚眦必报] %s 对 %s 的仇恨加深%n", characterId, targetId);
            relationship.changeIntensity(delta / 2, personality);
        }

        // 慷慨大方：给予帮助时，好感增加更多
        if (isGenerous() && delta > 10) {
            System.out.printf("  [慷慨大方] %s 对 %s 的好感额外增加%n", characterId, targetId);
            relationship.changeIntensity(5, personality);
        }
    }

    /**
     * 性格判断辅助方法
     */
    private boolean isVindictive() {
        return personality.getAgreeableness() <= 30 && personality.getNeuroticism() >= 70;
    }

    private boolean isGenerous() {
        return personality.getAgreeableness() >= 70 && personality.getOpenness() >= 60;
    }

    /**
     * 获取所有关系
     */
    public Map<String, UnidirectionalRelationship> getAllRelationships() {
        return new HashMap<>(relationships);
    }

    /**
     * 获取特定类型的关系
     */
    public List<UnidirectionalRelationship> getRelationshipsByType(RelationshipType type) {
        List<UnidirectionalRelationship> result = new ArrayList<>();
        for (UnidirectionalRelationship rel : relationships.values()) {
            if (rel.getType() == type) {
                result.add(rel);
            }
        }
        return result;
    }

    /**
     * 检查是否存在特定关系
     */
    public boolean hasRelationship(String targetId, RelationshipType type) {
        UnidirectionalRelationship relationship = relationships.get(targetId);
        return relationship != null && relationship.getType() == type;
    }

    // Getter方法
    public String getCharacterId() {
        return characterId;
    }

    public PersonalityTrait getPersonality() {
        return personality;
    }
}
